package com.duowei.diandan.db.entry;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;

import com.duowei.diandan.bean.food.FoodImpl;
import com.duowei.diandan.bean.food.Taste;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * 套餐设定
 */
@Entity
public class Tcsd implements FoodImpl, Serializable {
    @PrimaryKey(autoGenerate = true)
    private long id;

    /** 套餐编号(关联tcmc中的xmbh) */
    private String xmbh;
    /** 单品编号 */
    private String xmbh1;
    /** 单品名称 */
    private String xmmc1;
    /** 单位 */
    private String dw1;
    /** 数量 */
    private Float sl;
    /** 类别(A、B、C...) */
    private String tm;
    /** 单价 */
    private Float dj;
    /** 默认选择项(1默认选择) */
    private String sfxz;
    /** 是否套餐主项(1主项) */
    private String gq;
    private String bz;
    /** 类别编码 */
    private String lbbm;
    /** 序号(主键) */
    private Integer xh;

    // -----

    /**
     * 兑换该单品所需积分
     */
    public Float yhj;

    private String jyxmszTm;

    private String lbmc;

    @Ignore
    private Jyxmsz jyxmsz;

    /** 口味列表(可选) */
    @Ignore
    private List<Taste> tasteList;

    public Tcsd() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getXmbh() {
        return xmbh;
    }

    public void setXmbh(String xmbh) {
        this.xmbh = xmbh;
    }

    public String getXmbh1() {
        return xmbh1;
    }

    public void setXmbh1(String xmbh1) {
        this.xmbh1 = xmbh1;
    }

    public String getXmmc1() {
        return xmmc1;
    }

    public void setXmmc1(String xmmc1) {
        this.xmmc1 = xmmc1;
    }

    public String getDw1() {
        return dw1;
    }

    public void setDw1(String dw1) {
        this.dw1 = dw1;
    }

    public Float getSl() {
        return sl;
    }

    public void setSl(Float sl) {
        this.sl = sl;
    }

    public String getTm() {
        return tm;
    }

    public void setTm(String tm) {
        this.tm = tm;
    }

    public Float getDj() {
        if (this.dj == null) {
            return 0f;
        } else {
            return this.dj;
        }
    }

    public void setDj(Float dj) {
        this.dj = dj;
    }

    public String getSfxz() {
        return sfxz;
    }

    public void setSfxz(String sfxz) {
        this.sfxz = sfxz;
    }

    public String getGq() {
        return gq;
    }

    public void setGq(String gq) {
        this.gq = gq;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public String getLbbm() {
        return lbbm;
    }

    public void setLbbm(String lbbm) {
        this.lbbm = lbbm;
    }

    public Integer getXh() {
        return xh;
    }

    public void setXh(Integer xh) {
        this.xh = xh;
    }

    // -----

    public Float getYhj() {
        return yhj;
    }

    public void setYhj(Float yhj) {
        this.yhj = yhj;
    }

    public String getJyxmszTm() {
        return jyxmszTm;
    }

    public void setJyxmszTm(String jyxmszTm) {
        this.jyxmszTm = jyxmszTm;
    }

    public Jyxmsz getJyxmsz() {
        return jyxmsz;
    }

    public void setJyxmsz(Jyxmsz jyxmsz) {
        this.jyxmsz = jyxmsz;
    }

    public String getLbmc() {
        return lbmc;
    }

    public void setLbmc(String lbmc) {
        this.lbmc = lbmc;
    }

    public List<Taste> getTasteList() {
        return tasteList;
    }

    public void setTasteList(List<Taste> tasteList) {
        this.tasteList = tasteList;
    }

    public void cleanTasteChecked() {
        if (tasteList != null) {
            for (Taste t : tasteList) {
                t.setChecked(false);
            }
        }
    }

    public String getPz() {
        if (tasteList == null) {
            return null;
        } else {
            StringBuilder pz = new StringBuilder();
            for (int i = 0; i < tasteList.size(); i++) {
                Taste taste = tasteList.get(i);
                if (taste.isChecked()) {
                    pz.append("(").append(taste.getDmpzsd().getNr()).append(")");
                }
            }
            return pz.toString();
        }
    }

    // -----
    @Override
    public String getImgNo() {
        return this.jyxmszTm;
    }

    @Override
    public String getName() {
        return this.xmmc1;
    }

    @Override
    public Float getPrice() {
        Float price = getMemberPrice();
        if (price != null && price < this.dj) {
            return price;
        } else {
            return this.dj;
        }
    }

    @Override
    public Float getMemberPrice() {
        if (this.jyxmsz != null) {
            return this.jyxmsz.getMemberPrice();
        } else {
            return null;
        }
    }

    @Override
    public Float getXl() {
        if (this.jyxmsz != null) {
            return this.jyxmsz.getXl();
        } else {
            return null;
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Tcsd) {
            Tcsd t = (Tcsd) obj;

            return Objects.equals(this.xmbh, t.xmbh)
                    && Objects.equals(this.xmbh1, t.xmbh1)
                    && Objects.equals(this.xmmc1, t.xmmc1)
                    && Objects.equals(this.dw1, t.dw1)
                    && Objects.equals(this.sl, t.sl)
                    && Objects.equals(this.tm, t.tm)
                    && Objects.equals(this.dj, t.dj)
                    && Objects.equals(this.sfxz, t.sfxz)
                    && Objects.equals(this.gq, t.gq)
                    && Objects.equals(this.bz, t.bz)
                    && Objects.equals(this.lbbm, t.lbbm)
                    && Objects.equals(this.xh, t.xh)
                    && Objects.equals(this.yhj, t.yhj);
        } else {
            return false;
        }
    }

    public static String querySql() {
        return "select isnull(yhj, 0) yhj, TCSD.xmbh xmbh, TCSD.XMBH1 xmbh1, TCSD.XMMC1 xmmc1, " +
                "TCSD.DW1 dw1, TCSD.SL sl,TCSD.TM tm, TCSD.DJ dj, TCSD.SFXZ sfxz, TCSD.GQ gq, BZ bz, " +
                "TCSD.lbbm lbbm, jyxmsz.tm jyxmszTm, jyxmsz.lbmc lbmc from TCSD, JYXMSZ " +
                "where TCSD.XMBH1 = JYXMSZ.xmbh order by XH|";
    }
}
